package com.yuan.plugin.json;

import java.lang.reflect.Type;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class JsonHelper {

	/**
	 * 对象转json
	 * 
	 * @param object
	 * @return
	 */
	public static String object2Json(Object object) {
		Gson gson = new Gson();
		return gson.toJson(object);
	}

	/**
	 * json转对象
	 * 
	 * @param json
	 * @param classOfT
	 * @return
	 */
	public static Object json2Object(String json, Type typeOfT) {
		Gson gson = new GsonBuilder().create();
		Object object = null;
		try {

			object = gson.fromJson(json, typeOfT);
		} catch (Exception e) {
			Log.w("json2Object", e.toString());
		}

		return object;
	}

	/**
	 * map转JSON
	 * 
	 * @param map
	 * @return
	 */
	public static String map2Json(Map<String, String> map) {
		JSONObject object = new JSONObject(map);
		return object.toString();
	}

	public static String map2SingleArray(Map<String, String> map) {

		return "[" + map2Json(map) + "]";
	}

	public static boolean isSuccess(String object) {
		try {
			JSONObject jsonObject = new JSONObject(object);
			return jsonObject.getJSONObject("data").getBoolean("is_success");
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}
}